/* ************************************************************************************************

	Copyright (c) 2005-2006, IGR
	All Rights Reserved.
	Licensed under the Apache License version 2.0 or above.
	For more information on licensing, see:
        http://www.apache.org/licenses/


************************************************************************************************
 Objet type CALENDAR
 VERSION:0.85
 DATE:16/03/2006
************************************************************************************************
 Init parameter :
  top:           	 Top position (in pixel)
  left:              Left position (in pixel)    
  objPar              object of parameter
 		dateMin		     each date before is disable
		dateMax		     each date after  is disable
        currentDate	     current date when display calendar
        bOpenOnLastDate  if true the calendar will open on the last date selected
        bUkFormat	     if true the date will be like 'MM/DD/YYYY' (DD/MM/YYYY if not)
************************************************************************************************/

rialto.widget.Calendar=function(objPar) {   

	if(!objPar){var objPar={}};
	//base constructor    
    objPar.type="calendar";    
    this.base = rialto.widget.AbstractComponent;  
    this.base(objPar);
    //Default value
    
    this.dateMin=null;
    this.dateMax=null;
    this.bOpenOnLastDate=true;
    this.bUkFormat=false;
    this.popUpMode=false;
    this.arrDay=new Array(31);
    //today date   
    this.currentDate = rialto.date.toDay ();
	//set the value with the properties of the objPar object
    if (objPar != null){                           
        if (rialto.lang.isDate(objPar.dateMin)){
            this.dateMin = objPar.dateMin;        
        } 
        if (rialto.lang.isDate(objPar.dateMax)){
			this.dateMax = objPar.dateMax; 
            if(this.dateMin && this.dateMax<this.dateMin){
            	var d=this.dateMin;
            	this.dateMin=this.dateMax;
            	this.dateMax=d;	       
            }
        } 
        if (rialto.lang.isDate(objPar.currentDate)){
            this.currentDate= objPar.currentDate;        
        }
        if (rialto.lang.isBoolean(objPar.bOpenOnLastDate)){
            this.bOpenOnLastDate = objPar.bOpenOnLastDate;        
        }
        if (rialto.lang.isBoolean(objPar.popUpMode)){
            this.popUpMode = objPar.popUpMode;        
        }
        if (rialto.lang.isBoolean(objPar.bUkFormat)){
            this.bUkFormat = objPar.bUkFormat;        
        }          
    }

	this._SpecialDate=new Array,	

	this.setCurrent();
    this.createCalendar(objPar.parent);
    
    //release
    objPar=null;
    this.base=null;
}
rialto.widget.Calendar.prototype = new rialto.widget.AbstractComponent;

	/*************************PROPERTIES********************************/	
	
rialto.widget.Calendar.prototype._Holidays=new Array("01/01","01/05","08/05","14/07","15/08","01/11","11/11","25/12");
rialto.widget.Calendar.prototype._Month=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
rialto.widget.Calendar.prototype._Day=new Array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
rialto.widget.Calendar.prototype._LastDayInMonth= new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
rialto.widget.Calendar.prototype.DimanchePaques = false;

	/*************************METHODS********************************/	

rialto.widget.Calendar.prototype.isDate=function(wDate) {		
		return (wDate instanceof Date);		
	}

rialto.widget.Calendar.prototype.isHolidays=function(jf,mf) {
         var i=0
         mf+=1;
         for(var i in this._Holidays) {
             if ( test=(((jf<10)?'0'+jf:jf)+'/'+((mf<10)?'0'+mf:mf)) == this._Holidays[i++]) return true;}
         return false;
	}
	
rialto.widget.Calendar.prototype.isWeekEnd=function(jwe)  {
		 return ( ((jwe == 5) || (jwe == 6)) ? true : false ); 
	}
	
rialto.widget.Calendar.prototype.isToday=function(day)  {
		 var date1=new Date(this.currentYear,this.currentMonth,day);
		 return  rialto.date.equals(date1,rialto.date.toDay()); 
	}
		
rialto.widget.Calendar.prototype.isDisable=function(day)  {
		var dTest=new Date(this.currentYear,this.currentMonth,day);			
		var check=false;
    	if(rialto.lang.isDate(this.dateMin) && dTest<this.dateMin){check=true;}
	   	if(rialto.lang.isDate(this.dateMax) && dTest>this.dateMax){check=true;}
    	return check; 
	}
	
rialto.widget.Calendar.prototype.isPaques=function(pan){
         this.DimanchePaques = true;
         
         var b=pan-1900;
         var c=pan%19;        
         var d=Math.floor((7*c+1)/19);
         var e=(11*c+4-d)%29;
         var f=Math.floor(b/4);
         var g=(b+f+31-e)%7;
         var date=25-e-g;

         if (date>0) {mois = 4}
         else    {date=31+date; mois=3};
         date = ( (date<10)? '0'+date:date);
         this._Holidays.push( ((date<10)? '0'+date:date)+'/'+((mois<10)?'0'+mois:mois)); // Dimanche de Paques
         date+=1;
         if ( (date>31) && (mois=3)) {date=1;mois+=1}
         this._Holidays.push(((date<10)? '0'+date:date)+'/'+((mois<10)?'0'+mois:mois)); //Lundi de P?ques f?ri?
         date+=38;
         while (date>31) {var tt=0; date-=this._LastDayInMonth[mois-1+tt]; tt++ ; mois+=1}
         this._Holidays.push(((date<10)? '0'+date:date)+'/'+((mois<10)?'0'+mois:mois)); // Jeudi de l'ascencion
         date+=10;
         while (date>31) {var tt=0; date-=this._LastDayInMonth[mois-1+tt]; mois+=1}
         this._Holidays.push(((date<10)? '0'+date:date)+'/'+((mois<10)?'0'+mois:mois)); // Pentecote
	}
	
rialto.widget.Calendar.prototype.setCurrent=function(){
		this.currentDay = this.currentDate.getDate();
	    this.currentMonth = this.currentDate.getMonth();
	    this.currentYear = this.currentDate.getFullYear();	
	}	
	
rialto.widget.Calendar.prototype.setSpecialDate=function(date,color,text){
		this._SpecialDate.push([date,color,text]);
	}

rialto.widget.Calendar.prototype.removeSpecialDate=function(){
		this._SpecialDate=new Array;
	}	

rialto.widget.Calendar.prototype.isInSpecialDate=function(date){
		for(var i=0;i<this._SpecialDate.length;i++){
			//check if the 1st argument is a date or an interval
			if(rialto.lang.isDate(this._SpecialDate[i][0])){
				if(rialto.date.equals(this._SpecialDate[i][0],date)){
					return i;
				}
			}
			else{
				var inter=this._SpecialDate[i][0];
				if(date>=inter[0] && date<=inter[1]){
					return i;
				}		
			}
		}
		return -1;
	}
	
rialto.widget.Calendar.prototype.setMonthYear=function (m,a) {                           
         if (m>11) {
             m=0;
             a++;
         }
         if (m<0) {
             m=11;
             a--;
         }
    	this.currentMonth=m;
    	this.currentYear=a;    	        
        this.fillCalendar();
	}	
	
rialto.widget.Calendar.prototype.displayCalendar=function(top,left) {    	   
	    if(!this.bOpenOnLastDate){
	    	this.setCurrent();	    
	    }
        this.fillCalendar();
		if(this.popUpMode){
			//fixe top and left
			this.liste.setPosTop(top);
		    this.liste.setPosLeft(left);
	    	this.liste.affichezoneMenu();
    	}
	}
	
	
rialto.widget.Calendar.prototype.createCalendar=function(parent) {    	    
	    var oThis=this;	           
	    //GENERATION OF DOM ELEMENTS
	    // Global Area of the calendar
	    this.divExt.id="divExt";    
	    this.divExt.style.position="absolute";                
	    this.divExt.style.zIndex=1000;
	    this.divExt.style.width=200;               
	    //disable the onclick to prevent closing the calendar by clicking anywhere
	    this.divExt.onclick=function(e){
			if(!e)e=window.event;	    
	    	stopEvent(e);
	    }
	    
	    oParent = this.divExt;
	    //div deco
	    var divDeco=new objDiv3D('','cadreFenetreSansTirette','sansInnerHtml')
	    divDeco.createDiv3D (oParent);    
	    //Header with the navigation arrow
	    this.header=document.createElement("DIV");
	    this.header.style.width='100%';
	    this.header.style.height='30px';    
	    this.header.style.position='absolute';
	    this.header.style.top=0;
	    this.header.style.left=0;
	    this.divExt.appendChild(this.header);	    
	    oParent=this.header;	
	    // previous year button
	    this.btnPreviousYear = new rialto.widget.Image("btonFirstDoctOff",5,5,oParent,"Previous year","btonFirstDoctOn",{position:'absolute'});    
	    this.btnPreviousYear.onclick=function () {oThis.setMonthYear(oThis.currentMonth,oThis.currentYear-1);};
	    // previous month button
	    this.btnPreviousMonth = new rialto.widget.Image("leftArrowOff",25,5,oParent,"Previous month","leftArrowOn",{position:'absolute'});    
	    this.btnPreviousMonth.onclick=function () {oThis.setMonthYear(oThis.currentMonth-1,oThis.currentYear)};
	    //text of the current month and year
	    this.currentMonthYear =document.createElement("DIV");
	    this.currentMonthYear.className="Div_MoisEnCours";
	    this.header.appendChild(this.currentMonthYear);	    	
	    // next month button
	    this.btnNextMonth = new rialto.widget.Image("rightArrowOff",143,5,oParent,"Next month","rightArrowOn",{position:'absolute'});    
	    this.btnNextMonth.onclick=function () {oThis.setMonthYear(oThis.currentMonth+1,oThis.currentYear)};
	    // next year button
	    this.btnNextYear = new rialto.widget.Image("btonLastDoctOff",160,5,oParent,"Next year","btonLastDoctOn",{position:'absolute'});    
	    this.btnNextYear.onclick=function () {oThis.setMonthYear(oThis.currentMonth,oThis.currentYear+1)};	    
	    if(this.popUpMode){
	    	// close button
	    	this.btnClose=new rialto.widget.Image("redCloseButtonOff",180,5,oParent,"Close","grayCloseButtonOn",{position:'absolute'});  
	    	this.btnClose.onclick=function(){oThis.closeCalendar();}
		}
	    //day of the month
	    for (var j=0;j<7;j++) {
	        var LibelleCelluleCalendar=document.createElement("DIV");
	        LibelleCelluleCalendar.id="Calendar-Jour";
	        LibelleCelluleCalendar.className="Calendar-Jour";                 
	        LibelleCelluleCalendar.style.left=(5+(j*25))+"px";                
	        if(j!=6){LibelleCelluleCalendar.style.borderRight="1px solid #4AA5C9";}
	        this.header.appendChild(LibelleCelluleCalendar);
	        LibelleCelluleCalendar.innerHTML=this._Day[j];
	    }
	   
	    //Div in which we display the days
	    this.zoneCalendar=document.createElement("DIV");
	    this.zoneCalendar.style.width='100%';
	    this.zoneCalendar.style.position='absolute';
	    this.zoneCalendar.style.top=50;
	    this.zoneCalendar.style.left=0;
	    this.divExt.appendChild(this.zoneCalendar);
	    
	    if(this.popUpMode){    
	    	var obj={name:"MENU-CALENDAR",posFixe:true};
	    	this.liste=new rialto.widget.simpleMenu(obj);        
		    this.liste.add(this.divExt); 		    
    	}
    	else{
    		if (parent){    			
    			this.divExt.style.top=this.top;
    			this.divExt.style.left=this.left;
    			this.placeIn(parent);
    			this.fillCalendar();
    		}
    	}	    		    		    	    	    	        
}
	
rialto.widget.Calendar.prototype.afterOnClick=function(day,oHtml) {             
                
        //New current
        this.currentDate=new Date(this.currentYear,this.currentMonth,day);
        var clickDay=day<10 ? '0'+day:day;
        var clickMonth=this.currentMonth+1<10?'0'+(this.currentMonth+1):this.currentMonth+1;
        var clickYear=this.currentYear;
        //remove old current HTML style
        if(this.clickDiv){
			this.setSytleOfOneDay(this.clickDiv);			        		
		}  
		//New style for current
        if(oHtml){
			this.setSytleOfOneDay(oHtml);			        		
		}  
        //appel du code applicatif
        var strDate="/"+clickYear;        
        if(this.bUkFormat){
			strDate=clickMonth+"/"+clickDay+strDate
        }
        else{
   			strDate=clickDay+"/"+clickMonth+strDate
        }
       	this.onclick(strDate);        
        //ferme le Calendar
        if(this.popUpMode){this.closeCalendar();}
	}
	
rialto.widget.Calendar.prototype.onclick=function(dateSel) {
		//The code must be redefine after the instanciation of the component. By default this method does nothing. 
		//dateSel parameter is a string representing the select date on format la date selectionn?e au format jj/mm/aaaa
	}
	
rialto.widget.Calendar.prototype.closeCalendar=function() {    	   
		if(this.popUpMode){this.liste.fermezoneMenu();}
	}
	
rialto.widget.Calendar.prototype.fillCalendar=function() {
	    var oThis=this;
	    //clear the area
	    this.zoneCalendar.innerHTML="";	

	    //set the current year an month text
	    text=this._Month[this.currentMonth]+" "+this.currentYear;
	    this.currentMonthYear.innerHTML=text;   	    
	    //first day of the current year/month
	    this.firstDay = new Date(this.currentYear,this.currentMonth).getDay()-1;      
	  
	    if (rialto.date.isBissextile(this.currentYear)) {this._LastDayInMonth[1] = 29 ;} else {this._LastDayInMonth[1] = 28 ;}
	    if (!this.DimanchePaques) {this.isPaques(this.currentYear)};
	    
		var nbCol=Math.ceil((this.firstDay+this._LastDayInMonth[this.currentMonth])/7);		
		
	    if (nbCol>5) {
	        this.divExt.style.height=150; 
	    }
	    else {
	        this.divExt.style.height=135; 
	    }
	    
	    var oThis=this;		   
	
	    // Definition des zones "jour du mois"
	    for(s=0;s<nbCol;s++) {
	        for (j=0;j<7;j++) {	            
	            var day=7*s+j-this.firstDay+1;
	            var oneDay=document.createElement("DIV");    	            
	            oneDay.className="Calendar-zoneprincipale";
	            oneDay.style.position="absolute";
	            oneDay.style.left=(5+(j*25))+"px";
	            oneDay.style.top=(s*15)+"px";
   	            oneDay.style.width="25px";
		        oneDay.style.height="15px";
	            oneDay.borderStyle={};
	            
	            if(s<nbCol){
					oneDay.borderStyle.borderBottom="1px solid #4AA5C9";
	            	oneDay.style.borderBottom="1px solid #4AA5C9";
	            }
	            if(j!=6){
		            oneDay.borderStyle.borderRight="1px solid #4AA5C9";
	            	oneDay.style.borderRight="1px solid #4AA5C9";
	            }	            
	            
	            this.zoneCalendar.appendChild(oneDay);
	            
	            if(day>0 && day <=this._LastDayInMonth[this.currentMonth]){
					oneDay.id=day;
		            oneDay.innerHTML=day;
		            this.arrDay[day]=oneDay;
		            oneDay.onclick= function(){					                  				        			                		            	
		            	oThis.afterOnClick(this.id,this);						
		            }
		            
		            /*oneDay.onmouseover= function(){
		        		this.style.backgroundColor='#89D4F7';	                
		            }
		            
		            oneDay.onmouseout= function(){		     
			            this.style.backgroundColor='';       	
		        		oThis.setSytleOfOneDay(this);                            	
		            }*/
		            
		            this.setSytleOfOneDay(oneDay);
		            if (day==this._LastDayInMonth[this.currentMonth]) break;
		         }		       
	    	}   
		}
		oneDay=null;
	}	
	
rialto.widget.Calendar.prototype.setSytleOfOneDay=function(oneDay){
		
		var day=oneDay.id;
		//default
		var s =	oneDay.style;	
		s.fontWeight="";
        s.backgroundColor="";
        s.width="25px";        
        s.height="15px";
		s.borderTop="0px solid #4AA5C9";
		s.borderLeft="0px solid #4AA5C9";
		s.borderBottom="0px solid #4AA5C9";
		s.borderRight="0px solid #4AA5C9";
		s.color="#0069B3";
		
		for(prop in oneDay.borderStyle){		
			s[prop]=oneDay.borderStyle[prop];
		}

		if(this.isDisable(day)){
	       	//s.backgroundColor='#888888';
	        s.color='#AAAAAA';
   	        s.fontStyle='italic';
   	        s.cursor='default';
        }
        else{
            var date1=new Date(this.currentYear,this.currentMonth,day);
            
            
            if (this.isWeekEnd(j)) {
            	s.color="green"; 
            }
        	if (this.isHolidays(day,this.currentMonth) ) {
            	s.color="red"; 
            }
            if (this.isToday(day)) {
            	s.backgroundColor='#00CCFF';
                //s.color='white';
            }
            if (rialto.date.equals(date1,this.currentDate)) {
            	this.clickDiv=oneDay;
            	s.border='1px solid red';	                
            	s.height="13px";
            	s.width="23px";
            	s.fontWeight="bold";				
            }		                		                
            ind=this.isInSpecialDate(date1);
            if (ind!=-1) {			            
            	s.backgroundColor=this._SpecialDate[ind][1]; 
            	oneDay.title=this._SpecialDate[ind][2]; 
            }
        }        
	}

rialto.widget.Calendar.prototype.release=function(){	    
	    this.divExt.onclick=null;
	    for(i=0;i<32;i++){
	    	if(this.arrDay[i]){
		    	this.arrDay[i].onclick=null;
		    	this.arrDay[i]=null;	    	
	    	}
	    }
	    this.btnPreviousYear.remove();
	    this.btnPreviousYear.onclick=null;
	    this.btnPreviousMonth.remove();
	    this.btnPreviousMonth.onclick=null;
	    this.btnNextMonth.remove();
	    this.btnNextMonth.onclick=null;
	    this.btnNextYear.remove();
	    this.btnNextYear.onclick=null;
	    if(this.popUpMode){
		    this.liste.remove();
	    	this.btnClose.remove();
	    	this.btnClose.onclick=null;
		}
    	
}
	
rialto.widget.Calendar.prototype.getHtmlExt=function(){	    
    	return this.divExt;
	}
